package com.basketball.gym.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.basketball.gym.pojo.Information;
import com.basketball.gym.mapper.InformationMapper;
import com.basketball.gym.pojo.ResponseData;
import com.basketball.gym.pojo.User;
import com.basketball.gym.pojo.state.ResponseState;
import com.basketball.gym.service.InformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basketball.gym.utils.WrapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * Information实现类
 * </p>
 *
 * @author 赵肖云
 * @since 2021-03-21
 */
@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements InformationService {
    @Autowired
    private InformationMapper informationMapper;

    /**
     * 完善信息
     */
    @Override
    public ResponseData<Information> insertInformation(Information information, User user) {
        information.setUserId(user.getId());
        informationMapper.insert(information);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), information);
    }

    /**
     * 更新信息
     */
    @Override
    public ResponseData<Information> updateInformation(Information information, User user) {
        UpdateWrapper<Information> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id", user.getId());
        int isSuccess = informationMapper.update(information, wrapper);
        if (isSuccess == 1) {
            information = informationMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("user_id", user.getId()));
            return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), information);
        } else {
            return new ResponseData<>(ResponseState.USER_NOT_EXIST.getMessage(), ResponseState.USER_NOT_EXIST.getValue());
        }

    }

    /**
     * 查询信息
     */
    @Override
    public ResponseData<Information> getInformation(User user) {
        Information information = informationMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("user_id", user.getId()));
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), information);
    }
}
